package com.example.yc23.demo.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.yc23.demo.mapper.FilmMapper;
import com.example.yc23.demo.pojo.Film;
import com.example.yc23.demo.pojo.User;
import com.example.yc23.demo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;

@Service
public class FilmService implements IService<Film> {
    @Autowired
    private FilmMapper filmMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public BaseMapper<Film> getBaseMapper() {
        return this.filmMapper;
    }
    public JsonResult review(Integer filmId,String content,String token){
        JsonResult jsonResult=new JsonResult();
        if (token==null){
            jsonResult.setResponseCode(403);
            jsonResult.setResponseMsg("未登录无法评论");
        }else{
            Object obj=redisTemplate.opsForValue().get(token);
            if (obj==null){
                jsonResult.setResponseCode(403);
                jsonResult.setResponseMsg("令牌无效或已过期");
            }else{
                User currentUser=(User) obj;//成功的从redis中获得登录用户信息
                filmMapper.insertReview(currentUser.getId(),filmId,content,new Date());
                jsonResult.setResponseCode(200);
                jsonResult.setResponseMsg("评论成功");
            }
        }
        return jsonResult;
    }

    public JsonResult score(Integer filmId,Integer stars,String token){
        JsonResult jsonResult=new JsonResult();
        if (token==null){
            jsonResult.setResponseCode(403);
            jsonResult.setResponseMsg("未登录无法评分");
        }else{
            Object obj=redisTemplate.opsForValue().get(token);
            if (obj==null){
                jsonResult.setResponseCode(403);
                jsonResult.setResponseMsg("令牌无效或已过期");
            }else{
                User currentUser=(User) obj;//成功的从redis中获得登录用户信息
                if (filmMapper.selectCountScore(currentUser.getId(),filmId)==0){
                    filmMapper.insertScore(currentUser.getId(),filmId,stars);
                }else{
                    filmMapper.updateScore(currentUser.getId(),filmId,stars);
                }
                jsonResult.setResponseCode(200);
                jsonResult.setResponseMsg("评分成功");
            }
        }
        return jsonResult;
    }

    public Film getFilmDetail(Integer id){
        Film film=getById(id);
        film.setMakerInfo(getMakerInfo(id));
        film.setScore(filmMapper.selectAvgScoreById(id));
        film.setScoreDetails(getScoreDetails(id));
        return film;
    }
    public HashMap<String,Object> getScoreDetails(Integer id){
        HashMap<String,Object> scoremap=new HashMap();
        scoremap.put("total",filmMapper.selectScoreTotal(id));
        scoremap.put("onestar","0.0%");
        scoremap.put("twostar","0.0%");
        scoremap.put("threestar","0.0%");
        scoremap.put("fourstar","0.0%");
        scoremap.put("fivestar","0.0%");
        List<HashMap> list=filmMapper.selectScorePrecent(id);
        for (Map map: list){
            BigDecimal x= (BigDecimal) map.get("percent");
            float y=Math.round(x.floatValue()*1000)/10;
            if (map.get("star").equals(1)){
                scoremap.put("onestar",y+"%");
            }
            if (map.get("star").equals(2)){
                scoremap.put("twostar",y+"%");
            }
            if (map.get("star").equals(3)){
                scoremap.put("threestar",y+"%");
            }
            if (map.get("star").equals(4)){
                scoremap.put("fourstar",y+"%");
            }
            if (map.get("star").equals(5)){
                scoremap.put("fivestar",y+"%");
            }
        }
        return scoremap;
    }

    public HashMap<String,String> getMakerInfo(Integer id){
        List<HashMap> list=filmMapper.selectMakerInfo(id);
        HashMap<String,String> makerinfo=new HashMap();
        makerinfo.put("director","");//导演
        makerinfo.put("voicer","");//配音
        makerinfo.put("actor","");//演员
        for (HashMap map:list){
            if ("导演".equals(map.get("job"))){
                String s=makerinfo.get("director");
                if (s.length()>0)
                    s=s+"/";
                s=s+map.get("name");
                makerinfo.put("director",s);
            }
            if ("配音".equals(map.get("job"))){
                String s=makerinfo.get("voicer");
                if (s.length()>0)
                    s=s+"/";
                s=s+map.get("name");
                makerinfo.put("voicer",s);
            }
            if ("演员".equals(map.get("job"))){
                String s=makerinfo.get("actor");
                if (s.length()>0)
                    s=s+"/";
                s=s+map.get("name");
                makerinfo.put("actor",s);
            }
        }
        return makerinfo;
    }

    public Map addCastMember(Integer filmId,Integer makerId,String job){
        Map res=new HashMap();
        if (filmMapper.countFilmmaker(filmId,makerId,job)==0){
            if (filmMapper.insertFilmMarker(filmId,makerId,job)>0){
                res.put("isok",1);
            }
        }else{
            res.put("isok",0);
            res.put("msg","相同记录已存在，不允许重复添加");
        }
        return res;
    }

    @Override
    public boolean saveBatch(Collection<Film> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<Film> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<Film> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(Film entity) {
        if (entity.getId()==null){
            return save(entity);
        }else{
            return updateById(entity);
        }
    }

    @Override
    public Film getOne(Wrapper<Film> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<Film> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<Film> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public Class<Film> getEntityClass() {
        return null;
    }
}
